﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using DAL;
using Telerik.Web.UI;
using System.Collections.Generic;

public partial class TaoBaiViet : System.Web.UI.Page
{
    BAIVIETBLL bvBll = new BAIVIETBLL();
    TIENCANBLL tcBll = new TIENCANBLL();
    HOSOBENHANBLL hsbaBll = new HOSOBENHANBLL();
    TINHTRANGBVBLL ttBv = new TINHTRANGBVBLL();
    TINHTRANGHSBLL ttHs = new TINHTRANGHSBLL();
    TRIEUCHUNGBLL tchungBll = new TRIEUCHUNGBLL();
    SINHHIEUBLL shBll = new SINHHIEUBLL();
    THONGTINKHACBLL ttkBll = new THONGTINKHACBLL();
    CANLAMSANGBLL clsBll = new CANLAMSANGBLL();
    XETNGHIEMBLL xnBll = new XETNGHIEMBLL();
    CHITIET_DANHMUCBLL ctdmBll = new CHITIET_DANHMUCBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //from sinhhieu
        string txt_mach = ((TextBox)Hieusinh1.FindControl("TextBox_mach")).Text;
        string txt_nhiptho = ((TextBox)Hieusinh1.FindControl("TextBox_nhiptho")).Text;
        string txt_huyetap = ((TextBox)Hieusinh1.FindControl("TextBox_huyetap")).Text;
        string txt_nhietdo = ((TextBox)Hieusinh1.FindControl("TextBox_nhietdo")).Text;

        //from xetnghiem
        string str_label_xn1 = ((TextBox)Xetnghiem1.FindControl("txt_xetngiem1")).Text;
        string str_label_xn2 = ((TextBox)Xetnghiem1.FindControl("txt_xetngiem2")).Text;
        List<XNChoosed> listXn1 = Xetnghiem1.listXN1;
        List<XNChoosed> listXn2 = Xetnghiem1.listXN2;

        //from canlamsang
        string str_cls_sieuam = ((Label)Canlamsang1.FindControl("label_sieuam_change")).Text;
        string str_cls_noisoi = ((Label)Canlamsang1.FindControl("label_noisoi_change")).Text;
        string str_cls_xquang = ((Label)Canlamsang1.FindControl("label_xquang_change")).Text;

        //from thongtinkhac
        string str_thongtinkhac = ((Label)Thongtinkhac1.FindControl("label_ttk_change")).Text;

        //from trieuchung
        string str_trieuchung = ((Label)Trieuchung1.FindControl("label_trieuchung")).Text;

        //from tiencan
        string str_tiencan = ((Label)Tiencan1.FindControl("label_tiencan")).Text;

        //from ketluan
        string str_ketluan = ((Label)Ketluan1.FindControl("label_ketluan")).Text;

        //form noidung
        string str_noidung = label_conten_motahosobenhan.Text;

        
        THANHVIEN tv = (THANHVIEN)Session["thanhvienObject"];
        try
        {
            //create baiviet
            bvBll.insert(txt_chude.Text, str_noidung, Int32.Parse(Page.Request["maPhumuc"]), tv.mathanhvien, DateTime.Now, ttBv.UnBlock(), tv.mathanhvien.ToString(),0);
            BAIVIET newBv = bvBll.getBestNewBaiviet();
            string path_newbaiviet = Server.MapPath("./Baiviet/Source/" + newBv.mabaiviet.ToString());
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Sinhhieu/Images");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Sinhhieu/Files");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Tiencan/Images");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Tiencan/Files");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Xetngiem/Images");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Xetngiem/Files");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Trieuchung/Images");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Trieuchung/Files");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Thongtinkhac/Images");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Thongtinkhac/Files");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Canlamsang/Images");
            System.IO.Directory.CreateDirectory(path_newbaiviet + "/Canlamsang/Files");

            //create hosobenhan
            hsbaBll.insert(newBv.mabaiviet, str_ketluan, path_newbaiviet);
            HOSOBENHAN newHoso = hsbaBll.getBestNewHosobanhan();

            //insert tiencan
            tcBll.insert(str_tiencan, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Tiencan");

            //insert trieuchung
            tchungBll.insert(str_trieuchung, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Trieuchung");

            //insert sinhhieu
            shBll.insert(txt_nhiptho, txt_nhietdo, txt_huyetap, txt_mach, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Sinhhieu");

            //insert thongtinkhac
            ttkBll.insert(str_thongtinkhac, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Thongtinkhac");

            //insert canlamsang
            //for sieuam
            clsBll.insert(3, str_cls_sieuam, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Canlamsang");
            //for noisoi
            clsBll.insert(4, str_cls_noisoi, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Canlamsang");
            //for x-quang
            clsBll.insert(5, str_cls_xquang, newHoso.mabenhan, DateTime.Now, path_newbaiviet + "/Canlamsang");

            //insert xet nghiem
            if (!string.IsNullOrEmpty(str_label_xn1)) {

                //create xetnghiem
                xnBll.insert(newHoso.mabenhan, str_label_xn1, DateTime.Now, path_newbaiviet + "/Xetngiem");
                XETNGHIEM xn = xnBll.getBestXetnghiem();
                //create chitietxetnghiem
                if (listXn1.Count != 0)
                {
                    foreach (XNChoosed xnc in listXn1)
                    {
                        ctdmBll.insert(xnc.id_madanhmuc, xn.maxetnghiem, xnc.TextBox.Text);      
                    }
                }
            }
                if (!string.IsNullOrEmpty(str_label_xn2)){
                xnBll.insert(newHoso.mabenhan, str_label_xn2, DateTime.Now, path_newbaiviet + "/Xetngiem");
            }

            Response.Redirect("Baiviet.aspx?mabv=" + newBv.mabaiviet + "&tenbv=" + newBv.tenbaiviet);
        }
        catch { 
        
        }
    }
    protected void btn_edit_motabenhan_change_Click(object sender, EventArgs e)
    {
        p_motabenhan_change.Visible = false;
        p_motabenhan_end.Visible = true;
        rar_noidung.Content = label_conten_motahosobenhan.Text;
    }
    protected void btn_motabenhan_Click(object sender, EventArgs e)
    {
        p_motabenhan_change.Visible = true;
        p_motabenhan_end.Visible = false;
        label_conten_motahosobenhan.Text = rar_noidung.Content;
    }
}
